package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOssUtil;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 *  String ACCESS_KEY_ID="LTAI5tMFbS7KUkqpnZWLWMoK";
 *  String ACCESS_KEY_SECRET="fI2nLR381CquJ8w4SCfejY4rHrcVJM";
 * 功能：文件上传_本地存储
 * 作者：大风
 * 日期：2024/10/21 13:19
 */
@RestController
public class FileUploadController {
    @SneakyThrows
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename(); //获取原始文件名
        //保证文件的名字是唯一的，从而防止文件覆盖
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("C:\\Users\\Administrator\\Desktop\\临时\\files\\"+filename)); //存储到这里
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());

        return Result.success(url);
    }
}
